home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / r5update.zip / XCMS.C < prev   
C/C++ Source or Header  |  1991-10-05  |  3KB  |  129 lines

  1. /*
  2.  * xcms.c
  3.  *
  4.  * Written by David Flanagan.  Copyright 1991, O'Reilly && Associates.
  5.  * This program is freely distributable without licensing fees and
  6.  * is provided without guarantee or warranty expressed or implied.
  7.  * This program is -not- in the public domain.
  8.  *
  9.  * This program demonstrates the use of the Xcms color allocation
  10.  * functions and of the Xcms TekHVC color space gamut querying
  11.  * functions.  Invoke it with a Hue and a Chroma as command line
  12.  * arguments, and it will allocate and display 10 colors at that
  13.  * Hue and Chroma, with Value equally spaced between the minimum
  14.  * and maximum legal Values at that Hue and Chroma.  Hue must be
  15.  * between 0.0 and 360.0, and the maximum legal Chroma varies
  16.  * with Hue.  A Chroma of 30.0 or less should always be legal, and
  17.  * in no case may it exceed 100.0.
  18.  */
  19.  
  20. #include <stdio.h>    
  21. #include <X11/Xlib.h>
  22. #include <X11/Xcms.h>    
  23.  
  24. /*
  25.  * This routine allocates n shades of the color with specified Hue and
  26.  * Chroma.  The Value of each shade will be equally spaced between the
  27.  * minimum and maximum Values of the device gamut for the given Hue and
  28.  * Chroma.
  29.  */
  30. Status AllocShades(dpy, cmap, hue, chroma, pixels, n)
  31. Display *dpy;
  32. Colormap cmap;
  33. double hue, chroma;
  34. long *pixels;          /* RETURN */
  35. int n;
  36. {
  37.     XcmsColor color;
  38.     XcmsCCC ccc;
  39.     int i;
  40.     double minv, maxv;
  41.     double deltav;
  42.     
  43.     ccc = XcmsCCCOfColormap(dpy, cmap);
  44.     
  45.     if (XcmsTekHVCQueryMinV(ccc, hue, chroma, &color) == XcmsFailure)
  46.     return XcmsFailure;
  47.     else
  48.     minv = color.spec.TekHVC.V;
  49.     
  50.     if (XcmsTekHVCQueryMaxV(ccc, hue, chroma, &color) == XcmsFailure)
  51.     return XcmsFailure;
  52.     else
  53.     maxv = color.spec.TekHVC.V;
  54.     
  55.     if (n > 1) deltav = (maxv - minv)/(n-1);
  56.     else deltav = maxv - minv;
  57.     
  58.     for(i=0; i < n; i++) {
  59.     color.format = XcmsTekHVCFormat;
  60.     color.spec.TekHVC.H = hue;
  61.     color.spec.TekHVC.C = chroma;
  62.     color.spec.TekHVC.V = minv + i*deltav;
  63.     if (XcmsAllocColor(dpy, cmap, &color, XcmsRGBFormat) == XcmsFailure)
  64.         return XcmsFailure;
  65.     pixels[i] = color.pixel;
  66.     }
  67.     return XcmsSuccess;
  68. }
  69.  
  70.  
  71. void main(argc, argv)
  72. int argc;
  73. char *argv[];
  74. {
  75.     Display *dpy;
  76.     int scrn;
  77.     Window win;
  78.     GC gc;
  79.     Colormap cmap;
  80.     XWindowAttributes wa;
  81.     XEvent event;
  82.     long pixels[10];
  83.     double hue, chroma;
  84.     int i;
  85.  
  86.     if (argc != 3) {
  87.     (void) fprintf(stderr, "Usage: %s Hue Chroma\n", argv[0]);
  88.     exit(1);
  89.     }
  90.  
  91.     (void) sscanf(argv[1], "%lg", &hue);
  92.     (void) sscanf(argv[2], "%lg", &chroma);
  93.     
  94.     if ((dpy = XOpenDisplay(NULL)) == NULL) {
  95.     (void) fprintf(stderr, "%s: Can't open display.\n", argv[0]);
  96.     exit(1);
  97.     }
  98.     scrn = DefaultScreen(dpy);
  99.     win = XCreateSimpleWindow(dpy, RootWindow(dpy, scrn), 10, 10, 200, 400, 1,
  100.                   BlackPixel(dpy, scrn), WhitePixel(dpy, scrn));
  101.     gc = XCreateGC(dpy, win, 0, NULL);
  102.  
  103.     XGetWindowAttributes(dpy, win, &wa);
  104.     cmap = wa.colormap;
  105.     
  106.     if (AllocShades(dpy, cmap, hue, chroma, pixels, 10) == XcmsFailure) {
  107.     (void) fprintf(stderr, "%s: Couldn't allocate colors.\n", argv[0]);
  108.     exit(1);
  109.     }
  110.     
  111.     XSelectInput(dpy, win, ExposureMask);
  112.     XMapWindow(dpy, win);
  113.  
  114.     while(1) {
  115.     XNextEvent(dpy, &event);
  116.     switch(event.type) {
  117.     case Expose:
  118.         for(i=0; i<10; i++) {
  119.         XSetForeground(dpy,gc,pixels[i]);
  120.         XFillRectangle(dpy, win, gc, 0, 40*i, 200, 40);
  121.         }
  122.         break;
  123.     }
  124.     }
  125. }
  126.  
  127.  
  128.  
  129.